package com.cgbl.cg.im;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.netease.nimlib.sdk.auth.LoginInfo;
import com.netease.nimlib.sdk.qchat.result.QChatLoginResult;
import com.netease.yunxin.kit.common.ui.utils.ToastX;
import com.netease.yunxin.kit.corekit.im.IMKitClient;
import com.netease.yunxin.kit.corekit.im.login.LoginCallback;

public class ImManager {


    /**
     * start login page, you can use to launch your own login
     */
    public static void startLogin(String account, String token, LoginListener loginListener) {
        //填入你的 account and token
        //如果使用圈组，请置为true
        boolean userQChat = false;
        LoginInfo loginInfo = LoginInfo.LoginInfoBuilder.loginInfoDefault(account, token).build();

        if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(token)) {
            loginIM(loginInfo, userQChat, loginListener);
        } else {
//            activityWelcomeBinding.appDesc.setVisibility(View.GONE);
//            activityWelcomeBinding.loginButton.setVisibility(View.VISIBLE);
//            activityWelcomeBinding.loginButton.setOnClickListener(view -> launchLoginPage());
        }
    }

    /**
     * when your own page login success, you should login IM SDK
     */
    private static void loginIM(LoginInfo loginInfo, boolean useQChat, LoginListener loginListener) {
        if (useQChat) {
            //如果你只是用IM功能，可以使用 IMKitClient.loginIM() 完成登录
            //登录IM和圈组，需要开通IM和圈组功能
            IMKitClient.loginIMWithQChat(loginInfo, new LoginCallback<QChatLoginResult>() {
                @Override
                public void onError(int errorCode, @NonNull String errorMsg) {
                    loginListener.loginFailed();
                    Log.d("WelcomeActivity", "login errorCode:" + errorCode + errorMsg);
                    ToastX.showShortToast("login errorCode:" + errorCode + errorMsg);
                    launchLoginPage();
                }

                @Override
                public void onSuccess(@Nullable QChatLoginResult data) {
                    loginListener.loginSuccess();
                    showMainActivityAndFinish();
                }
            });
        } else {

            //如果只需要IM，则此处只需要登录IM即可。需要去除工程中qchatkit-ui相关依赖
            IMKitClient.loginIM(loginInfo, new LoginCallback<LoginInfo>() {
                @Override
                public void onError(int errorCode, @NonNull String errorMsg) {
                    loginListener.loginFailed();
                    Log.d("WelcomeActivity", "login errorCode:" + errorCode + errorMsg);
                    ToastX.showShortToast("login errorCode:" + errorCode + errorMsg);
                    launchLoginPage();
                }

                @Override
                public void onSuccess(@Nullable LoginInfo loginInfo) {
                    loginListener.loginSuccess();
                    showMainActivityAndFinish();
                }
            });
        }
    }

    private static void showMainActivityAndFinish() {
//        finish();
//        Intent intent = new Intent();
//        intent.setClass(this, MainActivity.class);
//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//        this.startActivity(intent);
    }

    /**
     * launch login activity
     */
    private static void launchLoginPage() {
        // jump to your own LoginPage here
        ToastX.showShortToast("请在WelcomeActivity类startLogin方法添加账号信息即可进入");
    }


    public interface LoginListener {

        void loginSuccess();

        void loginFailed();
    }
}
